<?php
class HomeAction extends CommonAction{
	public $usertype;	//1:主人(自己的主页)    2:访客(已登录的)   3:游客(未登录的)
	public $id;			//主页的ID get('id');
	public $uid;		//Session 的ID
	
	//id的检测及加载用户类型检测
	public function __construct(){
		$this->uid = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$idres = M()->query('SELECT UserId FROM users WHERE UserId='.$this->_get('id')); 	//请修改此处查询的ID及表名
		$this->id = $idres[0]['UserId'];													//还有此处的字段名
		if(!$this->id)
			$this->id = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		if(!$this->id)
			echo '<script>window.history.back(-1);</script>';
		$this->getusertype();
	}
	
	//判断用户类型
	public function getusertype(){
		if($this->id==$this->uid){
			$this->usertype=1;
		}else{
			if($this->uid)
				$this->usertype=2;
			else
				$this->usertype=3;
		}
	}
	//个人主页
    public function index(){
		$id = $this->id;//get('id')主页id
		$uid = $this->uid;
		//获取用户信息
		$user = new UsersModel($id);
		$userdetail = $user->userinfo;
		$this->assign('userdetail',$userdetail);
		//判断是否填写详细信息
		$res = M('Student')->where('UserId='.$id)->find();
		$this->assign('res',$res);
		//判断是否已创建团队
		$team=M('Team')->where('Captain='.$id)->find();
		$this->assign('team',$team);
		if($uid != $id){
			$this->assign('self','error');
		}
		//所在团队
		$inteam=M('Teammembers')->where('IsApproved=1 and UserId='.$id)->select();
		foreach($inteam as $row){
			$arr[]=M('Team')->where('Id='.$row['TeamId'])->find();
		}
		//知识竞赛成绩
		$paper=M('paper')->where('UserId='.$this->id)->order("Score desc")->find();
		$this->assign('paper',$paper);
		$this->assign('arr',$arr);
		$this->assign('inteam',$inteam);
		$this->assign('usertype',$this->usertype);
		//博文 学习资源
		$this->assign('id',$this->id);
		$this->display();
    }
	/*功能: 根据参数决定加载页面
	/ a1 博文 a2 学习资源
	*/
	public function a1(){
		$uid=$info->uid;
		$type=$this->_get('type');
		@$page = $this->_get('page');
		if(!intval($page))
			$page=1;
		$info = new InfoModel($this->_get('id'),$page,$type);
		if(!$info->uid)
			echo '<script>alert("非法操作");window.history.back(-1);</script>';
		$this->assign('uid',$info->uid);		//参赛历程
		$this->assign('list',$info->list);		//参赛历程
		$this->assign('pagenum',$info->pagenum);		//总页数
		$this->assign('page',$info->page);			//当前页
		$this->assign('usertype',$this->usertype);
		switch($type){
			case 1:
				$this->display('a1');
			break;			
			case 2:
				$this->display('a2');
			break;
		}
	}
	//头像
    public function editAvatar(){
		$list = M('Users')->where("UserId={$this->uid}")->find();
		$this->assign('list',$list);
		$this->display('editAvatar');
    }
    public function editAvatarAjax(){
			$uid= $this->uid;
			$OldAvatar = M('Users')->where("UserId={$uid}")->field('Avatar')->find();
			import('ORG.Net.UploadFile');
			import("ORG.Util.Image");
			$upload = new UploadFile();// 实例化上传类
			$upload->maxSize  = 1048576 ;// 设置附件上传大小
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','bmp');// 设置附件上传类型
			$upload->savePath = './UpFile/Img/Avatar/';// 设置附件上传目录
			$upload->thumb = true; 
			//设置需要生成缩略图的文件后缀 
			$upload->thumbPrefix = 's_';
			//设置缩略图最大宽度
			$upload->thumbMaxWidth = '165';
			//设置缩略图最大高度
			$upload->thumbMaxHeight = '165';
			//删除原图
			$upload->thumbRemoveOrigin = true;
			if(!$upload->upload()) {
				// 上传错误提示错误信息
				$this->error($upload->getErrorMsg());
			}else{
				// 上传成功 获取上传文件信息
				$info = $upload->getUploadFileInfo();
				$Avatar = 's_'.$info[0]['savename'];
				edit_avatar($uid,$Avatar,$OldAvatar);
			} 
		}
	public function editDetailInfo(){
		//修改详细信息
		$id = $this->uid;
		if(!$_POST){
			$list = new HomeModel($id);
			$detailInfo = $list->getDetailInfo();
			$detailInfo['BirthDate'] = explode('-',$detailInfo['BirthDate']);
			$detailInfo['EnterSchoolDate'] = explode('-',$detailInfo['EnterSchoolDate']);
			$detailInfo['GraduateDate'] = explode('-',$detailInfo['GraduateDate']);
			//获取专业类别
			$majorcate = $list->getMajorCate();
			$this->assign('MajorCate',$majorcate);
			$this->assign('detailInfo',$detailInfo);
			$this->display();
		}else{
			$arr['EduLevel'] = $this->_post('EduLevel');
			$arr['SchoolNumber'] = $this->_post('SchoolNumber');
			$arr['Gender'] = $this->_post('Gender');
			$arr['BirthDate'] = $this->_post('BirthDate').'-'.$this->_post('BirthDate2');
			$arr['EnterSchoolDate'] = $this->_post('EnterSchoolDate').'-'.$this->_post('EnterSchoolDate2');
			$arr['GraduateDate'] = $this->_post('GraduateDate').'-'.$this->_post('GraduateDate2');
			$arr['College'] = $this->_post('College');
			$arr['MajorCate'] = $this->_post('MajorCate');
			$arr['MajorName'] = $this->_post('MajorName');
			$arr['MSN'] = $this->_post('MSN');
			$arr['QQ'] = $this->_post('QQ');
			$arr['Fetion'] = $this->_post('Fetion');
			$arr['PostalCode'] = $this->_post('PostalCode');
			$arr['Address'] = $this->_post('Address');
			$arr['FixedPhone'] = $this->_post('FixedPhone');
			$arr['PersonalIntroduce'] = $this->_post('PersonalIntroduce');
			edit_studentdetail_info($id,$arr);
			$this->success('修改详细信息成功','index');
		}
		
	}
    public function editPassword(){
		$uid=$this->uid;
		if(!$this->_post("new_pass")){
			//加载修改界面
			$this->display('editPassword');
		}else{
			//判断原密码是否匹配
		 	$m = M('users')->where("UserId={$uid}")->find();
			$oldpass=md5(trim($this->_post('old_pass')));
			if($m['Password'] != $oldpass){
				$this->error('原始密码错误','editPassword',1);//跳转 or 弹窗?
			}
			if($this->_post('new_pass') != $this->_post('check_pass')){
				$this->error('两次密码不一致','editPassword',1);
			}else{
				//修改密码
				edit_pass($uid,md5($this->_post('check_pass')));
				$this->success('修改密码成功');
			}
		}
    }
	//基本信息
    public function editBaseInfo(){
		$uid=$this->uid;
		if(!$_POST){
			$user = M('users')->where("UserId={$uid}")->field('TrueName,PhoneNumber,Email')->find();
			$this->assign('user',$user);		
			$this->display('editBaseInfo');
		}else{
			$data=array(
				"TrueName" => "{$this->_post('TrueName')}",
				"PhoneNumber" => "{$this->_post('PhoneNumber')}",
				"Email" => "{$this->_post('Email')}",
			);
			edit_base_info($uid,$data);
			$this->success('修改基本信息成功');
        }
    }
    
    public function teamForm(){
		$user=M('Users')->where("UserId=".$this->uid)->find();
		$this->assign('user',$user);
		$this->display();
	}
	//添加附件
	public function proupfile(){
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 2*1024*1024 ;// 设置附件上传大小
		$upload->allowExts  = array('doc', 'rar', 'pdf','txt');// 设置附件上传类型
		$upload->savePath =  './UpFile/Doc/StudyResources/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			echo "<script>window.parent.uploadCallBack(false);</script>";
			$this->error($upload->getErrorMsg());
			
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			echo "<script>window.parent.uploadCallBack('{$info[0]['savename']}');</script>";
		}
	}
	//功能: 简历页面
	//描述: 个人信息遍历，提供简历输出表单
	public function addResume(){
		$id=$this->id;
		$Model=new Model();
		$list = $Model->query("SELECT 
		U.Email,U.OrgName,U.TrueName,U.OrgName,U.Avatar,U.LastLoginDate,U.PhoneNumber,
		S.EduLevel,S.GraduateDate,S.MajorName,S.BirthDate, 
		S.Address,S.PostalCode
		FROM users U,student S WHERE U.UserId = S.UserId and S.UserId = {$id}
			LIMIT 1");
		
		$list[0]['usertype']=$this->usertype;
		$info=M('resume')->where('UserId='.$id)->find();
		if($info){
			$list[0]['tag']=1;	
		}
		$this->assign('list',$list[0]);
		$this->assign('info',$info);
		$this->display();
	}
	//功能：添加方法，修改简历
	//描述：tag 1 时已经添加简历可修个 0 是可添加
	public function resumeCheck(){
			$resume= D('resume');
			$result = $resume->create();
			if (!$result){
				//如果创建失败 表示验证没有通过 输出错误提示信息
				exit($resume->getError());
			}else{
				$TxtCourse = $this->_post('TxtCourse');
				$Txtpratice = $this->_post('Txtpratice');
				$TxtAwards = $this->_post('TxtAwards');
				$TxtSelfValue = $this->_post('TxtSelfValue');
				$TxtjobIntention = $this->_post('TxtjobIntention');
				//验证通过 可以进行其他数据操作
				if(!$this->_post('tag')){
					$resume->execute("insert into 
						__TABLE__ (UserId,CreateDate,TxtCourse,Txtpratice,TxtAwards,TxtSelfValue,TxtjobIntention) 
						values ('{$this->id}',now(),'{$TxtCourse}','{$Txtpratice}','{$TxtAwards}','{$TxtSelfValue}','{$TxtjobIntention}')
						");
				}else{
					$resume->execute("update __TABLE__ set 
						CreateDate=now(),TxtCourse='{$TxtCourse}',Txtpratice='{$Txtpratice}',TxtAwards={$TxtAwards},TxtSelfValue='{$TxtSelfValue}',TxtjobIntention='{$TxtjobIntention}'
						where UserId='{$this->uid}' limit 1
						");
				}
			}
		$this->redirect('Student/Home/addResume');
	}
	//申请志愿者
	public function addVolunteer(){
		$Model=new Model();
		$info = $Model->query('SELECT Id,Name  FROM `industry` WHERE IsPublic=1');
		if($Model->query('SELECT Id  FROM `volunteer` WHERE UserId='.$this->uid)){
			$this->assign('tag',1);
		}
		$this->assign('info',$info);
		$this->display();
	}
	/*志愿者申请处理
		调用common.php 文件中的方法添加数据
	*/
	public function volunteerCheck(){
		$uid=$this->uid;
		$data['UserId']=$this->uid;
		$data['Specialty']=$this->_post('Specialty').','.$this->_post('Specialty2');
		$IndustryId=$this->_post('IndustryId');
		for($i=0;$i<count($IndustryId);$i++){
			$data['IndustryId'].=$IndustryId[$i].',';
		}
		if($this->_post('tag')){
			if(M('volunteer')->where('UserId='.$data['UserId'])->setField($data))
			$this->success('修改审核信息');
		}else{
			if(add_volunteer($data))
			$this->success('等待审核中');
		}
	}
}